package com.base.common.utils;

import com.base.common.ConfigConstants;
import com.base.common.enums.AppType;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;

/**
 * 推送工具类
 * 
 * @author Kevin Colin
 * @datetime 2015年8月21日下午7:52:56
 *
 */
public class PushUtils {
	public static NotificationTemplate NotificationTemplate(String context, int bid)
			throws Exception {
		NotificationTemplate template = new NotificationTemplate();
		if (bid == AppType.CJY.getValue()) {
			template.setAppId(ConfigConstants.CJY_APP_ID);
			template.setAppkey(ConfigConstants.CJY_APP_KEY);
			
		} else if(bid == AppType.YGJ.getValue()) {
			template.setAppId(ConfigConstants.YGJ_APP_ID);
			template.setAppkey(ConfigConstants.YGJ_APP_KEY);
		}
		template.setTitle("");
		template.setText("");
		template.setLogo("");
		template.setTransmissionType(1);
		template.setTransmissionContent("");
		// template.setPushInfo("actionLocKey", 2, "message", "sound",
		// "payload", "locKey", "locArgs", "launchImage");
		return template;
	}
	/**
	 * 单个app推送消息
	 * 
	 * @param cid
	 * @param context
	 * @param bid
	 * @return
	 * @throws Exception
	 */
	public static String pushtoSingle(String cid, String context, int bid) throws Exception{
		IGtPush push = null;
		if (bid == AppType.CJY.getValue()) {
			push = new IGtPush(ConfigConstants.HOST,ConfigConstants.CJY_APP_KEY, ConfigConstants.CJY_MASTER_SECRET);
		} else if(bid == AppType.YGJ.getValue()) {
			push = new IGtPush(ConfigConstants.HOST,ConfigConstants.YGJ_APP_KEY, ConfigConstants.YGJ_MASTER_SECRET);
		}

		 TransmissionTemplate template = TransmissionTemplate(context, bid);
//		LinkTemplate template = linkTemplateDemo();
//		 NotificationTemplate template = NotificationTemplate(context, bid);
		// NotyPopLoadTemplate template =NotyPopLoadTemplateDemo();

		SingleMessage message = new SingleMessage();
		message.setOffline(true);
		message.setOfflineExpireTime(48 * 1000 * 3600);
		message.setData(template);
		// message.setPushNetWorkType(1); //鏍规嵁WIFI鎺ㄩ�璁剧疆

//		List<Target> targets = new ArrayList<Target>();
		String appId = null;
		Target target1 = new Target();
//		Target target2 = new Target();
		if (bid == AppType.CJY.getValue()) {
			target1.setAppId(ConfigConstants.CJY_APP_ID);
			appId = ConfigConstants.CJY_APP_ID;
		} else if(bid == AppType.YGJ.getValue()) {
			target1.setAppId(ConfigConstants.YGJ_APP_ID);
			appId = ConfigConstants.YGJ_APP_ID;
		}
		// 添加client
		target1.setClientId(cid);
//		 target1.setAlias(Alias);
		IPushResult ret = null;
		try {
			ret = push.pushMessageToSingle(message, target1);
		} catch (RequestException e) {
			String requstId = e.getRequestId();
			return requstId;
		}

		return ret.getResponse().toString();
	}

	public static TransmissionTemplate TransmissionTemplate(String context, int bid)
			throws Exception {
		TransmissionTemplate template = new TransmissionTemplate();
		if (bid == AppType.CJY.getValue()) {
			template.setAppId(ConfigConstants.CJY_APP_ID);
			template.setAppkey(ConfigConstants.CJY_APP_KEY);
			
		} else if(bid == AppType.YGJ.getValue()) {
			template.setAppId(ConfigConstants.YGJ_APP_ID);
			template.setAppkey(ConfigConstants.YGJ_APP_KEY);
		}
		template.setTransmissionContent(context);
		template.setTransmissionType(0);
		
		return template;
	}

	private String cid;
	
	private String response;

	public String getCid() {
		return cid;
	}

	/**
	 * 获取响应信息
	 * 
	 * @return
	 */
	public String getResponse() {
		return response;
	}
	
	public void setCid(String cid) {
		this.cid = cid;
	}
	
	public void setResponse(String response) {
		this.response = response;
	}
}
